package com.bw.controller;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.bw.dao.OrderDao;
import com.bw.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
public class OrderController {
    @Autowired
    OrderService orderService;
    @RequestMapping("/find")
    public Object find(String nowDate) {
        List<Map> maps = orderService.find(nowDate);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status",0);
        jsonObject.put("msg","");
        JSONArray objects = new JSONArray();
        for (Map map : maps) {
            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("name",map.get("name"));
            jsonObject1.put("value",map.get("total"));
            objects.add(jsonObject1);
        }
        jsonObject.put("data",objects);
        return jsonObject;
    }

    @RequestMapping("/findPm")
    public Object findPm(String nowDate) {
        List<Map> pm = orderService.findPm(nowDate);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status",0);
        jsonObject.put("msg","");
        JSONObject dataObject = new JSONObject();
        JSONArray catObject = new JSONArray();
        for (Map map : pm) {
            catObject.add(map.get("name"));
        }
        JSONArray serObject = new JSONArray();
        JSONObject jsonObject1 = new JSONObject();
        JSONArray objects = new JSONArray();
       jsonObject1.put("name","金额");
        for (Map map : pm) {
            objects.add(map.get("total"));
        }
       jsonObject1.put("data",objects);
        serObject.add(jsonObject1);
        dataObject.put("categories",catObject);
        dataObject.put("series",serObject);
        jsonObject.put("data",dataObject);
        return jsonObject;
    }

    @RequestMapping("/findCc")
    public Object findCc(String nowDate) {
        List<Map> cc = orderService.findCc(nowDate);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status",0);
        jsonObject.put("msg","");
        for (Map map : cc) {
            jsonObject.put("data",map.get("total"));
        }
        return jsonObject;
    }

    @RequestMapping("/findFz")
    public Object findFz(String nowDate) {
        List<Map> fz = orderService.findFz(nowDate);
        JSONObject jsonObject = new JSONObject();
    jsonObject.put("status",0);
    jsonObject.put("msg","");
        JSONObject dataObject = new JSONObject();
        JSONArray colObjects = new JSONArray();
        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("name","序号");
        jsonObject1.put("id","subsys");
        colObjects.add(jsonObject1);
        JSONObject jsonObject2 = new JSONObject();
        jsonObject2.put("name","名字");
        jsonObject2.put("id","name");
        colObjects.add(jsonObject2);
        JSONObject jsonObject3 = new JSONObject();
        jsonObject3.put("name","总额");
        jsonObject3.put("id","value");
        colObjects.add(jsonObject3);

        JSONArray rowObjects = new JSONArray();
        for (Map map : fz) {
            JSONObject jsonObject4 = new JSONObject();
            jsonObject4.put("subsys",map.get("id"));
            jsonObject4.put("name",map.get("name"));
            jsonObject4.put("value",map.get("sum"));
            rowObjects.add(jsonObject4);
        }
        dataObject.put("columns",colObjects);
        dataObject.put("rows",rowObjects);
        jsonObject.put("data",dataObject);
        return jsonObject;
    }
}
